package com.playcrab.adouban;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.Toast; 

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.douban.CollectionEntry;
import com.google.gdata.data.douban.MiniblogEntry;
import com.google.gdata.util.ServiceException;

public class EditCollection extends BaseActivity {
	private static final String LOG_TAG = "EditCollection";
	private Handler handler = new Handler();
	private String selectedStatus = null;
	
	public Map<String,String> getStatusesByType(String type){
		/*book:wish, reading, read
		movie:wish, watched
		tv:wish, watching, watched
		music:wish, listening, listened*/
		Map<String,String>  ss = new HashMap<String,String>();
	  
	  
	  if(type.equalsIgnoreCase("book")){
		  ss.put("wish","想读");
		  ss.put("reading","正在读");
		  ss.put("read","读过"); 
	  } 
	   else  if(type.equalsIgnoreCase("movie")){
		   ss.put("wish","想看");
		   ss.put("watched","看过");  
	   }
	   else if(type.equalsIgnoreCase("music")){
		   ss.put("wish","想听");
		   ss.put("listening","正在听");
		   ss.put("listened","听过"); 
	   } 
	   else if(type.equalsIgnoreCase("tv")){
		   ss.put("wish","想看");
		   ss.put("watching","正在看");
		  ss.put("watched","看过");  
	   }
	   return ss; 
		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		
		setContentView(R.layout.edit_collection);
		checkLoggedin();
		
		
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		final String type  = bundle.getString("type");
		final String id  = bundle.getString("id");
		final String title  = bundle.getString("title");
		 
		setTitle(getString(R.string.title_edit_collection).concat(" ").concat(title));
	
		final EditText contentEditText = (EditText) findViewById(R.id.edit_collection_content_edit);
		final Map<String,String> statues = getStatusesByType(type);
		final RadioGroup statusGroup = (RadioGroup) findViewById(R.id.edit_collection_status_group);
		final RatingBar ratingBar = (RatingBar)findViewById(R.id.edit_collection_rating);
		RadioButton rb=null;
		for(final String s:statues.keySet()){ 
			
			rb = new RadioButton(this); 
			
			rb.setText(statues.get(s));    
			rb.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener(){ 
				@Override
				public void onCheckedChanged(CompoundButton b, boolean checked) {
					// TODO Auto-generated method stub
					if(checked){
						selectedStatus = s;
						if(selectedStatus.equals("wish")){
							ratingBar.setVisibility(RatingBar.INVISIBLE);
	 					}else{
	 						ratingBar.setVisibility(RatingBar.VISIBLE);	
	 					}
					}
					
				}  
			}); 
			statusGroup.addView(rb);
			
		} 
		if(rb!=null){
			rb.toggle();// automatically select the last status option
		}
        
        final Button button = (Button) findViewById(R.id.edit_collection_button);
		OnClickListener listener = new OnClickListener() {
			public void onClick(View v) { 
				final String[] tags = ((EditText) findViewById(R.id.edit_collection_tag_edit)) .getText().toString().split(",");
			 
			 	 
				final String content = ( (EditText)findViewById(R.id.edit_collection_content_edit) ).getText().toString();
			 
			 	final int rating = (int) ratingBar.getRating();
			   boolean bprivacy = false;
				if(((CheckBox)findViewById(R.id.edit_collection_privacy_checkbox)).isChecked() ){
					bprivacy = true;
				}
				final boolean privacy = bprivacy;
 				button.refreshDrawableState(); 
				final DoubanService myService = getAuthedDoubanService();
				if(myService == null){
					return;
				} 
 				final Toast t = Toast.makeText(getApplicationContext(), 
						EditCollection.this.getResources().getText(R.string.compose_miniblog_failed),
						Toast.LENGTH_SHORT);
					new Thread() {
						public void run() { 
							handler.post(new Runnable() {
								public void run() {
									CollectionEntry entry;
									try {
										entry = myService.createCollection(selectedStatus, id,tags, rating, content, privacy);
										 
										if(entry != null){
											t.setText(EditCollection.this.getResources().getText(R.string.compose_miniblog_sent)); 
											Intent i = new Intent(EditCollection.this, Main.class);
											startActivity(i);
										}
									} catch (IOException e) { 
										e.printStackTrace(); 
										EditCollection.this.showInfo(getString(R.string.network_error));
									} catch (ServiceException e) {
										e.printStackTrace();
										EditCollection.this.showInfo(getString(R.string.service_error));
									} finally {
										t.show();
									//	button.setText(ComposeMiniblog.this.getResources().getText(R.string.compose_miniblog_button_text ) );
									
										//	progressDialog.dismiss();
									}
								}
							} );
						}
					}.start();
					
				
			}
		};
		
		
		button.setOnClickListener(listener);

	}

	public void onStart() {
		super.onStart();
	}

	public void onStop() {
		super.onStop();
	}
}
